package fr.oz.view.profile.dragable 
{
	import caurina.transitions.Tweener;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import fr.oz.core.datas.ProfilListDatas;
	import fr.oz.core.events.ClockEvent;
	import fr.oz.core.utils.Utils;
	import fr.oz.view.profile.dragable.ui.Aiguille;
	import fr.oz.view.profile.dragable.ui.Aiguille_B;
	import fr.oz.view.profile.dragable.ui.Aiguille_S;
	import fr.oz.view.profile.dragable.ui.ColorHandler;
	import fr.oz.view.profile.dragable.ui.ColorHandler_MC;
	import fr.oz.view.profile.dragable.ui.HorlogeBackground;
	import fr.oz.view.profile.dragable.ui.HourViewer;
	
	/**
	 * ...
	 * @author Fazermokeur
	 */
	public class DragableClock extends DragableClock_FC
	{
		
		private var _profil:ProfilListDatas;
		private var clockHour:Number;
		private var oldClockHour:Number;
		private var clockMinute:Number;
		
		private var _tmpRotHour:Number;
		private var currentAiguille:Aiguille;
		private var AigTaille:int;
		private var isDown:Boolean;
		private var angle:Number;
		private var dY:Number;
		private var dX:Number;
		
		private var bulle:ColorHandler;
		
		private var timeCalage:Number;
		
		public function DragableClock(profil:ProfilListDatas) 
		{
			_profil = profil;
			clockHour = Number(_profil.time_hour);
			clockMinute = Number(_profil.time_minut);
			addEventListener(Event.ADDED_TO_STAGE, init);
			
			var bg:HorlogeBackground = new HorlogeBackground();
			addChildAt(bg, 0);
			bg.cacheAsBitmap = true;
			mask_bg.cacheAsBitmap = true;
			bg.mask = mask_bg;
		}
		
		private function init(e:Event):void 
		{
			removeEventListener(Event.ADDED_TO_STAGE, init);
			addEventListener(Event.REMOVED_FROM_STAGE, dispose);
			
			this.graphics.beginFill(0xff0000);
			this.graphics.drawRect(0,0,4,4);
			this.graphics.endFill();
			initClock();
			
			bulle = new ColorHandler_MC();
			bulle.mouseChildren = false;
			bulle.mouseEnabled = false;
			bulle.alpha = 0;
			bulle.iselected = false;
			addChild(bulle);
			
			addEventListener(MouseEvent.MOUSE_DOWN, handleDown);
			stage.addEventListener(MouseEvent.MOUSE_UP, handleUp);
		}
		
		private function handleUp(e:MouseEvent):void 
		{
			if (isDown) {
				currentAiguille.upHandler();
			}
			
			bulle.iselected = false;
			
			if (isDown) {
				removeEventListener(Event.ENTER_FRAME, getRotation);
				isDown = false;
				dispatchEvent(new ClockEvent(ClockEvent.CLOCK_RELEASE, Utils.addZero(""+clockHour), Utils.addZero(""+clockMinute)));
			}
			
			if (currentAiguille is Aiguille_S)	timeCalage = 4;
			else timeCalage = 5;
			caleHour();
		}
		
		
		private function caleHour():void
		{
			var big_rotation:int = _tmpRotHour + ((360 * clockMinute / 60) * 30) / 360;
			//trace(_tmpRotHour);
			if(_tmpRotHour >= 180)	Tweener.addTween(aiguille_big_mc, {rotation:big_rotation-_tmpRotHour-(360-_tmpRotHour), time:timeCalage, onComplete:endCaleHour } );
			if (_tmpRotHour < 180)	Tweener.addTween(aiguille_big_mc, { rotation:big_rotation, time:timeCalage, onComplete:endCaleHour } );
			addEventListener(Event.ENTER_FRAME, animCircular);
		}
		
		private function endCaleHour():void
		{
			removeEventListener(Event.ENTER_FRAME, animCircular);
		}
		
		private function handleDown(e:MouseEvent):void 
		{
			if (e.target is Aiguille)
			{
				if (e.target is Aiguille_B) {
					currentAiguille = e.target as Aiguille_B;
					bulle.x = 100;
					bulle.y = 50;
					AigTaille = 72;
				}else {
					currentAiguille = e.target as Aiguille_S;
					bulle.x = 100;
					bulle.y = 100;
					AigTaille = 150;
				}
				
				bulle.visible = true;
				bulle.iselected = true;
				bulle.x = Math.cos(Math.PI * (angle-90) / 180) * AigTaille;
				bulle.y = Math.sin(Math.PI * (angle-90) / 180) * AigTaille;
				bulle.chooseColor(0xffddff);
				
				angle = currentAiguille.rotation;
				addEventListener(Event.ENTER_FRAME, getRotation);
				isDown = true;
			}
		}
		
		/**
		 * Rotation de l'Aiguille + maj de l'affichage de l'heure 
		 * La mise à jour de l'heure courante ne se fait que sur 12h AM
		 * @param	evt
		 */
		private function getRotation ( evt : Event ) : void
		{
			angle = Math.atan2(mouseY, mouseX) * 180 / Math.PI + 90;
			angle = angle < 0 ? Math.round(360 + angle) : Math.round(angle);
			//currentAiguille.rotation = angle;
			
			bulle.x = int(bulle.x + (dX - bulle.x) * .5);
			bulle.y = int(bulle.y + (dY - bulle.y) * .5);
			
			dX = Math.cos(Math.PI * (angle-90) / 180) * AigTaille;
			dY = Math.sin(Math.PI * (angle-90) / 180) * AigTaille;
			
			//if(currentAiguille is Aiguille_B)	hourCircle.traceCircle((angle % 30)*12);
			updateCurrentHour();
				
			//dispatchEvent(new Event(GlobalEvents.NEW_CLOCK, true));
		}
		
		private function updateCurrentHour():void
		{
			/*	Gestion des heures	*/
			if (currentAiguille is Aiguille_B)
			{
				clockHour = Math.round(((angle - 15) * 12) / 360);
				//if (!isAm)	clockHour += 12;
				//mouvements gradués
				//currentAiguille.rotation = (clockHour) * 30;
				currentAiguille.rotation = angle;
				
				moveRadAiguille();
				/*if ((clockHour == 11 && oldClockHour == 0) || (clockHour == 23 && oldClockHour == 12)) {
					isAm = !isAm;
				}
				if ((clockHour == 0 && oldClockHour == 11) || (clockHour == 12 && oldClockHour == 23)){
					isAm = !isAm;
					//clockHour += 12;
				}*/
				//Conaitre la position de l'aiguille
				_tmpRotHour = angle - (angle % 30);
			}
			/*	Gestion des minutes	*/
			else
			{
				/*	Je supprime 3 degré pour limité le passage à la secondes 60 */
				clockMinute = Math.round(((angle-3) * 60) / 360);
				moveRadAiguille();
				//mouvements gradués
				//currentAiguille.rotation = (clockMinute) * 6;
				currentAiguille.rotation = angle;
			}
			oldClockHour = clockHour;
		}
		
		private function dispose(e:Event):void 
		{
			removeEventListener(Event.REMOVED_FROM_STAGE, dispose);
			//OzLogger.debug(""+e.target);
			
			
		}
		
		/**
		 * Placement initial des aiguilles
		 */
		private function initClock():void 
		{
			var big_rotation:Number;
			if (clockHour > 11) big_rotation = (360 * (clockHour - 12) / 12);
			else big_rotation = (360 * clockHour / 12);
			var small_rotation : Number = (360 * clockMinute / 60);
			_tmpRotHour = big_rotation;
			big_rotation += (small_rotation*30/360);
			
			Tweener.addTween(aiguille_big_mc, { rotation:big_rotation, time:2, onComplete:endAnim} );
			Tweener.addTween(aiguille_small_mc, { rotation:small_rotation, time:1} );
			
			currentAiguille = null;
			addEventListener(Event.ENTER_FRAME, animCircular);
			
			dispatchEvent(new ClockEvent(ClockEvent.CLOCK_RELEASE, Utils.addZero(""+clockHour), Utils.addZero(""+clockMinute)));
		}
		
		private function animCircular(e:Event):void 
		{
			moveRadAiguille();
		}
		
		/**
		 * Mise à jour des cercles de background
		 */
		private function moveRadAiguille():void {
			var myAngle:int;
			if (aiguille_big_mc.rotation < 0)	myAngle = 360 + aiguille_big_mc.rotation;
			else 	myAngle = aiguille_big_mc.rotation;
			hourCircle.traceCircle(myAngle);
			if (aiguille_small_mc.rotation < 0)	myAngle = 360 + aiguille_small_mc.rotation;
			else 	myAngle = aiguille_small_mc.rotation;
			minutCircle.traceCircle(myAngle);
			
			bulle.putName(Utils.addZero(String(clockHour)) + ":" + Utils.addZero(String(clockMinute)));
		}
		
		/**
		 * Dispatch nouvelle heures
		 */
		private function endAnim():void {
			removeEventListener(Event.ENTER_FRAME, animCircular);
			
			
		}
		
		public function get hour():Number { return clockHour; }
		public function get minut():Number { return clockMinute; }
		
	}

}